package com.example.demo.controller;

import com.example.demo.po.Order;
import com.example.demo.service.OrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/orders")
public class OrderController {

    @Resource
    private OrderService orderService;

    @GetMapping("/{id}")
    public Order getById(@PathVariable Long id) {
        return orderService.getById(id);
    }

    @GetMapping
    public List<Order> getAll() {
        return orderService.getAll();
    }

    @GetMapping("/user/{userId}")
    public List<Order> getByUserId(@PathVariable Long userId) {
        return orderService.getByUserId(userId);
    }

    @PostMapping
    public boolean create(@RequestBody Order order) {
        return orderService.create(order);
    }

    @PutMapping("/{id}")
    public boolean update(@PathVariable Long id, @RequestBody Order order) {
        order.setId(id);
        return orderService.update(order);
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        return orderService.deleteById(id);
    }
}